<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- <div class="layui-row"> -->
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </page-filter>
    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :row-key="rowKey"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :reserve-selection="true"
      :checked-row-list="checkedRowList"
      :checkedList="checkBox ? checkedList : []"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-assetName="scope">
        {{ scope.row.assetName }}
      </template>
    </dialog-table>
    <!-- </div> -->
  </page-dialog>
</template>
<script setup name="selectAsset">
import PageDialog from '../PageDialog/index'
import DialogTable from '../DialogTable/index'
import PageFilter from '../NewPageFilter/index'
import { getCustomizeSetting } from '@/api/customize'
import { getMeasuringInstrumentCustomizePage } from '@/api/equipment.js'
import { getDeptAll } from '@/api/core'
const emits = defineEmits()
const { proxy } = getCurrentInstance()

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getMeasuringInstrumentCustomizePage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  tableKey: {
    type: String,
    default: 'AdverseEventModifyMeasuringInstrument-com.slip.spd.entity.vo.asset.settings.MeasuringInstrumentVO',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: proxy.sortString,
    queryMap: {
      status:{
        queryType: 'ALL_LIKE',
        value: '1'
      },
      enableStatus:{
        queryType: 'ALL_LIKE',
        value: '1'
      }
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: props.fieldList,
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择记录器具',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

setData()
function setData() {
  let obj = {
    statusValue: {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'adverse_event_status',
      show: false,
      hidden: true,
    },
    createUserName: {
      label: '报告人',
      value: 'createUserName',
      asValue: 'createUser',
      componentType: 'select-user',
      minWidth: '200px',
      highlightRow: true,
      checkBox: false,
      show: false,
      hidden: true,
    },
    createDeptName: {
      label: '报告部门',
      value: 'createDeptName',
      asValue: 'createDept',
      query: {
        deptAuth: 'id',
        status: 1,
      },
      api: getDeptAll,
      componentType: 'slip-select',
      show: false,
      hidden: true,
    },
    createTime: {
      label: '报告时间',
      value: 'createTime',
      componentType: 'dateTime',
      minWidth: '200px',
      show: false,
      hidden: true,
    },
    deviceName: {
      label: '设备名称',
      value: 'deviceName',
      minWidth: '200px',
      componentType: 'input',
      type: 'column',
      hidden: true,
    },
    deviceCode: {
      label: '设备编码',
      value: 'deviceCode',
      minWidth: '200px',
      componentType: 'input',
      type: 'column',
      hidden: true,
    },
    rejectReason: {
      label: '驳回原因',
      value: 'rejectReason',
      minWidth: '200px',
      componentType: 'input',
      type: 'column',
      hidden: true,
    },
    productName: {
      label: '产品名称',
      value: 'productName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
  }
  console.log(proxy.$fieldfList.FieldList)
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
// 获取列表
function getList() {
  proxy.$nextTick(() => {
    console.log(123)
    tableInfo.value.refresh = Math.random()
  })
}
function search() {
  const _tableInfo = tableInfo.value
  _tableInfo.initCurpage = Math.random()
  _tableInfo.refresh = Math.random()
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  console.log(event)
  console.log(data)
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let _tableInfo = tableInfo.value
  let _filterInfo = filterInfo.value
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        _filterInfo.searchList,
        data.detail
      )
      _filterInfo.searchList = searchInfo.searchList
      _filterInfo.searchList.forEach((item) => {
        item.event = search
      })

      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
        ...props.query,
      }
      filterInfo.value.query.queryParams = {
        ...filterInfo.value.query.queryParams,
        ...props.queryParams,
      }
      tableInfo.value.api = props.api
      getList()
    }
  })
}
</script>

<style scoped lang="scss"></style>
